package com.example.book.Service;

import com.example.book.Mapper.BookInfoMapper;
import com.example.book.enums.Status;
import com.example.book.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BookService {
    @Autowired
    BookInfoMapper bookInfoMapper;

    public PageResult<BookInfo> getBookList(PageRequest pageRequest) {
        pageRequest.setOffset();
        List<BookInfo> list = bookInfoMapper.getBookList(pageRequest.getOffset(), pageRequest.getLimit());
        Integer count = bookInfoMapper.getCount();
        //设置StatusName
        for(BookInfo bookInfo : list) {
            bookInfo.setStatusName(Status.getDescByCode(bookInfo.getStatus()));
        }
        return new PageResult<BookInfo>(list, count, pageRequest);
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.InsertBook(bookInfo);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public BookInfo queryBookById(Integer id) {
        return bookInfoMapper.queryBookById(id);
    }

    public Integer deleteBook(BookInfo bookInfo) {

        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
